package cn.com.cifi.mars.service.impl;


import cn.com.cifi.mars.bean.vo.sys.req.UpdateSysDeliveryPlanParam;
import cn.com.cifi.mars.bean.vo.sys.rsp.SysDeliveryPlanVo;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.constant.HrOrgTypeEunm;
import cn.com.cifi.mars.entity.common.SysDeliveryPlan;
import cn.com.cifi.mars.entity.common.SysProjectFetchNumber;
import cn.com.cifi.mars.mapper.mars.SysDeliveryPlanMapper;
import cn.com.cifi.mars.service.SysDeliveryPlanService;
import cn.com.cifi.mars.util.UUIDUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 推送供货计划表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-21
 */
@Service
public class SysDeliveryPlanServiceImpl extends ServiceImpl<SysDeliveryPlanMapper, SysDeliveryPlan> implements SysDeliveryPlanService {

	@Autowired
	private SysDeliveryPlanMapper sysDeliveryPlanMapper;

	/**
	 * 显示推送计划信息
	 */
	@Override
	public List<SysDeliveryPlanVo> querySysDeliveryPlan() {
		List<SysDeliveryPlanVo>voList=new ArrayList<SysDeliveryPlanVo>();
		QueryWrapper<SysDeliveryPlan>queryWrapper=new QueryWrapper<>();
		List<SysDeliveryPlan>list=sysDeliveryPlanMapper.selectList(queryWrapper);
		list.forEach(x->{
			SysDeliveryPlanVo vo=new SysDeliveryPlanVo();
			BeanUtils.copyProperties(x, vo);
			voList.add(vo);
		});
		return voList;
	}

	/**
	 * 导入推送计划表
	 */
	@Transactional
	@Override
	public int add() {
		QueryWrapper<SysDeliveryPlan>queryWrapper=new QueryWrapper<>();
		sysDeliveryPlanMapper.delete(queryWrapper);
		List<Map<String,Object>>list=sysDeliveryPlanMapper.selectRegionList(Constants.XUHUI_GROUP,Constants.ORGSTATUS,HrOrgTypeEunm.ORG_20.getKey());
		list.forEach(x->{
			String regionName=x.get("regionName").toString();
			String regionId=x.get("regionId").toString();
			SysDeliveryPlan bean=new SysDeliveryPlan();
			bean.setRegionId(regionId);
			bean.setRegionName(regionName);
			bean.setId(UUIDUtils.create());
			sysDeliveryPlanMapper.insert(bean);
		});
		return 1;
	}

	/**
	 * 设置推送时间
	 */
	@Transactional
	@Override
	public int update(UpdateSysDeliveryPlanParam param) {
		SysDeliveryPlan bean=sysDeliveryPlanMapper.selectById(param.getId());
		if(null==bean) {
			throw new MsgException(CodeEnum.SYS_PUSH_ERROR);
		}
		bean.setPushTime(param.getPushTime());
		int upd=sysDeliveryPlanMapper.updateById(bean);
		return upd;
	}
	
	
}
